// pages/index/index.js
import request from "../../utils/request.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    // 轮播图
    banners: [],
    // 推荐歌单
    recommendPlayList: [],
    rankList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const bannerData = await request("/banner", { type: 2 });
    const recommendPlayListData = await request("/personalized", { limit: 10 });
    this.setData({
      banners: bannerData.banners,
      recommendPlayList: recommendPlayListData.result,
    });
    const rankListData = await request("/toplist");
    const rankListIds = rankListData.list.slice(0, 5).map((item) => item.id);
    const rankListDetail = [];

    // 采用await的时候 一定不要使用异步函数循环遍历
    for (let item of rankListIds) {
      const data = await request("/playlist/detail", { id: item });
      let rankListItem = {
        name: data.playlist.name,
        tracks: data.playlist.tracks.slice(0, 3),
      };
      rankListDetail.push(rankListItem);
      // 发完请求马上更新数据  减少白屏时间
      this.setData({
        rankList: rankListDetail,
      });
    }
  },
  toRecommend() {
    wx.navigateTo({
      url: "/pages/index/recommend/recommend",
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
